<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>教师课表</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
    <script>
        $(document).ready(function(){
            var user = window.localStorage.getItem("username");
            var data = $.parseJSON(user);
            if(data.user_type !== 2){
                alert("您不是教师，无法访问此页面");
                window.location.href = "user_courses.html";
            }

            $("#scheduleForm").submit(function(event){
                event.preventDefault();
                var formData = $(this).serialize();
                formData += "&teacher_user_id=" + data.teacher_id;
                $.post("http://localhost:80/edu/teacher/schedule", formData, function(response) {
                    var rs = $.parseJSON(response);
                    if (rs.code != "00000") {
                        alert("获取课表数据失败");
                        return;
                    }
                    displaySchedule(rs.data);
                });
            });
        });

        function displaySchedule(schedule) {
            var table = $("#scheduleTable");
            table.empty();
            table.append("<tr><th>课程名称</th><th>上课时间</th><th>课程编号</th><th>学生人数</th><th>操作</th></tr>");
            $.each(schedule, function(index, course) {
                var row = $("<tr></tr>");
                row.append($("<td></td>").text(course.course_name));
                row.append($("<td></td>").text(course.course_time));
                row.append($("<td></td>").text(course.course_code));
                row.append($("<td></td>").text(course.current_section_count));
                var viewDetails = $('<button>查看详情</button>').click(function(){
                    showStudentInfo(course.course_code);
                });
                row.append($("<td></td>").append(viewDetails));
                table.append(row);
            });
        }

        function showStudentInfo(course_code) {
            var user = window.localStorage.getItem("username");
            var data = $.parseJSON(user);
            $.post("http://localhost:80/edu/teacher/schedule/student-info", {teacher_user_id: data.teacher_id, course_code: course_code}, function(response) {
                var rs = $.parseJSON(response);
                if (rs.code != "00000") {
                    alert("获取学生信息失败");
                    return;
                }
                var students = rs.data;
                var container = $("<div></div>");
                $.each(students, function(index, student) {
                    var studentInfo = $("<div></div>").text("学号: " + student.student_user_id + ", 姓名: " + student.student_realname + ", 班级: " + student.class_name + ", 年级: " + student.grade_name);
                    container.append(studentInfo);
                    var scoreInput = $("<input type='number' min='0' max='100' placeholder='输入分数'/>").data('student_id', student.student_user_id).data('course_code', course_code);
                    var scoreButton = $('<button>打分</button>').click(function(){
                        var score = $(this).prev().val();
                        scoreStudent(score, student.student_user_id, course_code);
                    });
                    container.append(scoreInput);
                    container.append(scoreButton);
                });
                container.appendTo(".container");
            });
        }

        function scoreStudent(score, student_user_id, course_code) {
            var user = window.localStorage.getItem("username");
            var data = $.parseJSON(user);
            $.post("http://localhost:80/edu/teacher/check-score", {user_score: score, student_user_id: student_user_id, course_code: course_code}, function(response) {
                var rs = $.parseJSON(response);
                if (rs.code == "00000") {
                    alert("打分成功");
                } else {
                    alert("打分失败：" + rs.message);
                }
            });
        }
    </script>
    <style>
        .container {
            text-align: center;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>教师课表</h1>
    <a href="user_courses.html">我的课程</a><br>
    <a href="all_blogs.html">校园论坛</a><br>
    <a href="change_pwd.html">修改密码</a><br>
    <a href="student_schedule.html">学生课表</a><br>
    <form id="scheduleForm">
        学年：<input type="text" name="course_year"><br>
        学期：<input type="text" name="course_semester"><br>
        <input type="submit" value="查看课表">
    </form>
    <table id="scheduleTable" border="1">
        <thead>
        <tr>
            <th>课程名称</th>
            <th>上课时间</th>
            <th>课程编号</th>
            <th>学生人数</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>
</body>
</html>